//
//  ZPBaseTabBarController.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2016/11/23.
//  Copyright © 2016年 liwei. All rights reserved.
//

#import "ZPBaseTabBarController.h"
#import "ZPBaseNavigationController.h"
#import "ZPCommunityViewController.h"
#import "ZPActivityViewController.h"
#import "ZPMineViewController.h"
#import "ZPTabBar.h"
#import "CourseListViewController.h"
#import "MessageNextViewController.h"
#import "SettingViewController.h"

#define iPone5TabBar_Height 64
#define iPone6TabBar_Height 75
#define iPone6plusTabBar_Height 83

#define zTabBarItemHeight 49

@interface ZPBaseTabBarController () 

//@property (nonatomic, strong) NSMutableArray *items;

@property (copy, nonatomic)   NSArray *tabBarItemImages;

@property (copy, nonatomic)   NSArray *tabBarItemTitles;

@property (strong, nonatomic) UIView  *centerItemBackgroundView;

@end

@implementation ZPBaseTabBarController

#pragma mark - <懒加载>
//- (NSMutableArray *)items{
//    if (_items == nil) {
//        
//        _items = [NSMutableArray array];
//    }
//    return _items;
//}

- (NSArray *)tabBarItemImages {
    if (_tabBarItemImages == nil) {
        _tabBarItemImages = @[@"tabbar_community", @"tabbar_course",@"",@"tabbar_message",@"tabbar_setting"];
    }
    return _tabBarItemImages;
}

- (NSArray *)tabBarItemTitles {
    if (_tabBarItemTitles == nil) {
       _tabBarItemTitles = @[@"社区", @"课表/签到",@"消息",@"设置"];
    }
    return _tabBarItemTitles;
}

- (UIView *)centerItemBackgroundView {
    if (_centerItemBackgroundView == nil) {
        CGFloat BgView_W = Main_Screen_Width / 5;
        
        if (iPhone6) { // 375,667
            CGFloat BgView_Y = 26;
            _centerItemBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(BgView_W * 2, BgView_Y, BgView_W, 49)];
            
        }else if (iPhone6_Plus) { //414,736
            CGFloat BgView_Y = 30;
            _centerItemBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(BgView_W * 2+0.5, BgView_Y, BgView_W, 54)];
            
        }else {// 320,568   320,480
            CGFloat BgView_Y = 22;
            _centerItemBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(BgView_W * 2, BgView_Y, BgView_W, 42)];
        }
        _centerItemBackgroundView.backgroundColor = zMainColor;
    }
    return _centerItemBackgroundView;
}

#pragma mark - <viewDidLoad>
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加tabBar上的子控制器
     [self setupAllChildViewControllers];
    // 设置tabBar
    //  [self setupTabBar5];
    [self setupTabBar4];
    
    /**********************************/
//    [self setupAllChildViewControllers1];
//    [self setupAllTabBarButton];
//    [self addActivityButton];
    
  
}

#pragma mark - <viewWillAppear>
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
# pragma mark - <修改自定义tabBar的字体大小>
/**
 系统默认的tabbar标题文字小，设置它的大小需要在
 +(void)load   或者在 +(void)initialize 里设置
 */
//+ (void)initialize {
//
//    // 获取当前类的tabBarItem
//    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
//    // 设置所有item的选中时颜色
//    // 设置选中文字颜色
//    // 创建字典去描述文本
//    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
//    // 文本颜色 -> 描述副文本属性的key -> NSAttributedString.h
//    attr[NSForegroundColorAttributeName] = KMainColor;
//    [item setTitleTextAttributes:attr forState:UIControlStateSelected];
//    // 通过normal状态设置字体大小
//    // 字体大小 跟 normal
//    NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
//    // 设置字体
//    attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
//    [item setTitleTextAttributes:attrnor forState:UIControlStateNormal];
//
//}
 
#pragma mark - <自定义tabBar>
/** 自定义tabBar的高度 */
//- (void)viewWillLayoutSubviews {
//    CGRect tabFrame = self.tabBar.frame;
//    tabFrame.size.height = 60;
//    tabFrame.origin.y = kScreen_Height - 60;
//    self.tabBar.frame = tabFrame;
//}


#pragma mark - <设置自定义tabBar上中间圆形按钮>
/** 设置中间圆形按钮 */
//- (void)addActivityButton {
//    UIButton * activity = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    // 设置未点击状态和点击状态下的图片,使用未被渲染的图片
//    [activity setImage:[UIImage imageWitnOriginalRenderingMode:@"tabbar_mainbtn"] forState:UIControlStateNormal];
//    [activity setImage:[UIImage imageWitnOriginalRenderingMode:@"fabu_pressed"] forState:UIControlStateHighlighted];
//    
//    // 自适应,自动根据按钮图片和文字计算按钮尺寸,将activity的frame的size调整为与内容image的size一致
//    [activity sizeToFit];
//    
//    // 设置center
//    activity.center = CGPointMake(self.tabBar.xj_width * 0.5, self.tabBar.xj_height * 0.5 - 10.0);
//    
//    // 添加target
//    [activity addTarget:self action:@selector(clickActivityBtn) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.tabBar addSubview:activity];
//}
//
//- (void)clickActivityBtn {
//    ZPActivityViewController *activityVc = [[ZPActivityViewController alloc] init];
//    [self.navigationController pushViewController:activityVc animated:YES];
//}


/** 自定义tabBar */
//- (void)setUpTabBar{
//    ZPTabBar *tabBar = [[ZPTabBar alloc] initWithFrame:self.tabBar.frame];
//
//    tabBar.delegate = self;
//    tabBar.items = self.items;
//    [self.view addSubview:tabBar];
//    // 移除系统的tabBar
//    [self.tabBar removeFromSuperview];
//}


#pragma mark - <设置tabBar上所有的按钮>
// 设置tabBar上按钮的内容
//- (void)setupAllTabBarButton {
//    
//    // 首页
//    ZPHomePageViewController *homeVc = self.childViewControllers[0];
//    [self setupChildViewController:homeVc title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home"];
//    
//    // 选课
//    ZPCourseViewController *courseVc = self.childViewControllers[1];
//    [self setupChildViewController:courseVc title:@"选课" image:@"tabbar_course" selectedImage:@"tabbar_course"];
//    
//    // 活动
//    ZPActivityViewController *activityVc = self.childViewControllers[2];
////    activityVc.tabBarItem.enabled = NO;
//    
//    // 社区
//    ZPCommunityViewController *communityVc = self.childViewControllers[3];
//    [self setupChildViewController:communityVc title:@"社区" image:@"tabbar_community" selectedImage:@"tabbar_community"];
//    
//    // 我的
//    ZPMineViewController *mineVc = self.childViewControllers[4];
//    [self setupChildViewController:mineVc title:@"我的" image:@"tabbar_mine" selectedImage:@"tabbar_mine"];
//    
//    // tabBar主色调
////    self.tabBar.barStyle = UIBarStyleBlack;
////    self.tabBar.barTintColor = zMainColor;
//
//    // 调整tabBarBtn的位置
//    UIEdgeInsets insets = UIEdgeInsetsMake(-2, 0, 2, 0);
//    UIEdgeInsets activityInsets = UIEdgeInsetsMake(10, 0, 0, 0);
//    
//    homeVc.tabBarItem.imageInsets       = insets;
//    courseVc.tabBarItem.imageInsets     = insets;
//    activityVc.tabBarItem.imageInsets   = activityInsets;
//    communityVc.tabBarItem.imageInsets  = insets;
//    mineVc.tabBarItem.imageInsets       = insets;
//}


// 抽取成一个方法,传入控制器、标题、正常状态下图片、选中状态下图片直接调用这个方法
//- (void)setupChildViewController:(UIViewController *)viewController  title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName {
//    
//    viewController.tabBarItem.title = title;
//    
//    // tabBar的图片颜色不要被系统渲染，显示原始颜色
//    viewController.tabBarItem.image = [UIImage imageWitnOriginalRenderingMode:imageName];
//    viewController.tabBarItem.selectedImage = [UIImage imageWitnOriginalRenderingMode:selectedImageName];
//    
//    // tabBar的标题文字颜色不要被系统渲染,显示自定义的颜色
//    NSDictionary *SelectedTitleColor = [NSDictionary dictionaryWithObject:zWhiteColor forKey:NSForegroundColorAttributeName];
//    [viewController.tabBarItem setTitleTextAttributes:SelectedTitleColor forState:UIControlStateSelected];
//    
//    // tabBar的标题文字自定义大小
//    NSDictionary *dictTitleFont = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13.0] forKey:NSFontAttributeName];
//    [viewController.tabBarItem setTitleTextAttributes:dictTitleFont forState:UIControlStateNormal];
//}


#pragma mark - <tabBae添加所有子控制器>
// tabBar上添加所有的子控制器
//- (void)setupAllChildViewControllers1 {
//    
//    // home
//    ZPHomePageViewController *homeVc = [[ZPHomePageViewController alloc] init];
//    ZPBaseNavigationController *homeNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:homeVc];
//    [self addChildViewController:homeNvc];
//    
//    // Disscover
//    ZPCourseViewController *courseVc = [[ZPCourseViewController alloc] init];
//    ZPBaseNavigationController *courseNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:courseVc];
//    [self addChildViewController:courseNvc];
//    
//    // activity
//    ZPActivityViewController * activityVc = [[ZPActivityViewController alloc] init];
//    ZPBaseNavigationController * activityNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:activityVc];
//    [self addChildViewController:activityNvc];
//    
//    // chat
//    ZPCommunityViewController *communityVc = [[ZPCommunityViewController alloc] init];
//    ZPBaseNavigationController *communityNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:communityVc];
//    [self addChildViewController:communityNvc];
//    
//    // Mine
//    ZPMineViewController * mineVc = [[ZPMineViewController alloc] init];
//    ZPBaseNavigationController * mineNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:mineVc];
//    [self addChildViewController:mineNvc];
//}

/************************************************** RDVtabbarController ************************************************/

#pragma mark - <tabBae添加所有子控制器>
- (void)setupAllChildViewControllers {
    
    //社区
    ZPCommunityViewController *communityVc = [[ZPCommunityViewController alloc] init];
    ZPBaseNavigationController *communityNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:communityVc];
    communityVc.urlStr = CommunityH5Url;
    communityVc.titleStr = @"社区";
    communityVc.isPush = NO;
    communityNvc.delegate = self;
    
    //课表签到
    CourseListViewController  *courseListVc = [[CourseListViewController  alloc] init];
    ZPBaseNavigationController *courseListNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:courseListVc];
    courseListNvc.delegate = self;
    
    //消息
    MessageNextViewController  *messageVc = [[MessageNextViewController  alloc] init];
    ZPBaseNavigationController *messageNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:messageVc];
    messageNvc.delegate = self;
    
    // Mine
    SettingViewController  *settingVc = [[SettingViewController alloc] init];
    ZPBaseNavigationController * settingNvc = [[ZPBaseNavigationController alloc] initWithRootViewController:settingVc];
    settingNvc.delegate = self;
    
    self.viewControllers = @[communityNvc,courseListNvc,messageNvc,settingNvc];
}

#pragma mark - <设置tabBar>
- (void)setupTabBar5 {
    
    self.tabBar.translucent = YES; // 半透明
    [self.tabBar setBackgroundColor:clear_color]; // 清除颜色
    //1.iPhone4分辨率320x480，像素640x960，@2x
    //2.iPhone5分辨率320x568，像素640x1136，@2x
    //3.iPhone6分辨率375x667，像素750x1334，@2x
    //4.iPhone6 Plus分辨率414x736，像素1242x2208，@3x
    if (Main_Screen_Width == 375) {
        self.tabBar.frame = CGRectMake(0, 0, Main_Screen_Width, iPone6TabBar_Height);
        
    }else if (Main_Screen_Width == 414) {
        self.tabBar.frame = CGRectMake(0, 0, Main_Screen_Width, iPone6plusTabBar_Height);
        
    }else {
        self.tabBar.frame = CGRectMake(0, 0, Main_Screen_Width, iPone5TabBar_Height);
    }
    
    NSInteger index = 0;
    UIImage *selectedImage   = Z_IMAGE(@"tabbar_selected_background"); // 选中背景图
    UIImage *unselectedImage = Z_IMAGE(@"");                           // 未选中背景图
    
    for (RDVTabBarItem * item in [[self tabBar] items]) {
        [item setBackgroundSelectedImage:selectedImage withUnselectedImage:unselectedImage];
        [item setBackgroundColor:zMainColor];
        if (index == 2) {
            [item setFinishedSelectedImage:Z_IMAGE(@"tabbar_activity") withFinishedUnselectedImage:Z_IMAGE(@"tabbar_activity")];
            
            ViewBorderRadius(item, Main_Screen_Width/5/2, 1, zFreeColor(29, 149, 213, 0.8));
            item.imagePositionAdjustment = UIOffsetMake(0, -5);// 调整图片位置
            item.titlePositionAdjustment = UIOffsetMake(0, 3); // 调整标题位置
            [self.tabBar insertSubview:self.centerItemBackgroundView atIndex:1];
        } else {
            
            if (Main_Screen_Width == 375) {
               item.itemHeight = 49;
                
            }else if (Main_Screen_Width == 414) {
                item.itemHeight = 53;
                
            }else {
                item.itemHeight = 42;
            }
            item.titlePositionAdjustment = UIOffsetMake(0, 3); // 调整标题位置
            switch (index) {
                case 0:
                    [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[0]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[0])];
                    break;
                    
                case 1:
                    [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[1]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[1])];
                    break;
                    
                case 3:
                    [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[3]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[3])];
                    break;
                    
                case 4:
                    [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[4]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[4])];
                    break;

            }
        }
    
        [item setTitle:self.tabBarItemTitles[index]]; // 添加标题
        item.selectedTitleAttributes = @{
                                         NSFontAttributeName:ZB11,
                                         NSForegroundColorAttributeName:white_color
                                         };
        item.unselectedTitleAttributes = @{
                                           NSFontAttributeName:ZB11,
                                           NSForegroundColorAttributeName:white_color
                                           };
        index++;
    }
}

- (void)setupTabBar4
{
//    self.tabBar.translucent = YES; // 半透明
    [self.tabBar setBackgroundColor:clear_color]; // 清除颜色
    self.tabBar.frame = CGRectMake(0, 0, Main_Screen_Width, 49);
    
    NSInteger index = 0;
    UIImage *selectedImage   = Z_IMAGE(@"tabbar_selected_background"); // 选中背景图
    UIImage *unselectedImage = Z_IMAGE(@"");                           // 未选中背景图

    for (RDVTabBarItem * item in [[self tabBar] items]) {
        [item setBackgroundSelectedImage:selectedImage withUnselectedImage:unselectedImage];
        [item setBackgroundColor:zMainColor];
        item.titlePositionAdjustment = UIOffsetMake(0, 2); // 调整标题位置
        item.imagePositionAdjustment = UIOffsetMake(0, -2);// 调整图片位置
        if (index== 0) {
            [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[0]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[0])];
        } else if (index == 1) {
            [item setFinishedSelectedImage:Z_IMAGE(self.tabBarItemImages[1]) withFinishedUnselectedImage:Z_IMAGE(self.tabBarItemImages[1])];
        } else if (index == 2) {
            [item setFinishedSelectedImage:Z_IMAGE(@"tabbar_message") withFinishedUnselectedImage:Z_IMAGE(@"tabbar_message")];
        } else if (index == 3) {
            [item setFinishedSelectedImage:Z_IMAGE(@"tabbar_setting") withFinishedUnselectedImage:Z_IMAGE(@"tabbar_setting")];
        }
        [item setTitle:self.tabBarItemTitles[index]]; // 添加标题
        item.badgeTextFont = [UIFont  systemFontOfSize:8.];
        item.selectedTitleAttributes = @{
                                         NSFontAttributeName:ZB12,
                                         NSForegroundColorAttributeName:white_color
                                         };
        item.unselectedTitleAttributes = @{
                                           NSFontAttributeName:ZB12,
                                           NSForegroundColorAttributeName:white_color
                                           };
        index++;
    }
}

#pragma mark - <UINavigationControllerDelegate代理方法>
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.viewControllers.count == 1) {
            [self setTabBarHidden:NO animated:NO]; // tabbar显现
    } else {
        [self setTabBarHidden:YES animated:YES]; // tabBar隐藏
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
